﻿@using Nop.Services.Catalog
@model ProductModel
@if (Model.Id > 0)
    {
        if (Model.NumberOfAvailableManufacturers > 0)
        {
            <div id="productmanufacturers-grid"></div>

            <script>
                $(document).ready(function () {
                    $("#productmanufacturers-grid").kendoGrid({
                        dataSource: {
                            type: "json",
                            transport: {
                                read: {
                                    url: "@Html.Raw(Url.Action("ProductManufacturerList", "Product", new { productId = Model.Id }))",
                                    type: "POST",
                                    dataType: "json"
                                },
                                create: {
                                    url: "@Html.Raw(Url.Action("ProductManufacturerInsert", "Product", new { productId = Model.Id }))",
                                    type: "POST",
                                    dataType: "json"
                                },
                                update: {
                                    url:"@Html.Raw(Url.Action("ProductManufacturerUpdate", "Product"))",
                                    type: "POST",
                                    dataType: "json"
                                },
                                destroy: {
                                    url: "@Html.Raw(Url.Action("ProductManufacturerDelete", "Product"))",
                                    type: "POST",
                                    dataType: "json"
                                }
                            },
                            schema: {
                                data: "Data",
                                total: "Total",
                                errors: "Errors",
                                model: {
                                    id: "Id",
                                    fields: {
                                        Manufacturer: { editable: true, type: "string" },
                                        //ProductId: { editable: false, type: "number" },
                                        ManufacturerId: { editable: true, type: "number" },
                                        IsFeaturedProduct: { editable: true, type: "boolean" },
                                        DisplayOrder: { editable: true, type: "number" },
                                        Id: { editable: false, type: "number" }
                                    }
                                }
                            },
                            requestEnd: function (e) {
                                if (e.type == "create" || e.type == "update") {
                                    this.read();
                                }
                            },
                            error: function (e) {
                                display_kendoui_grid_error(e);
                                // Cancel the changes
                                this.cancelChanges();
                            },
                            serverPaging: true,
                            serverFiltering: true,
                            serverSorting: true
                        },
                        pageable: {
                            refresh: true,
                            numeric: false,
                            previousNext: false,
                            info: false
                        },
                        toolbar: ["create"],
                        edit: function(e) {
                            if (e.model.isNew()) {
                                //little hack here
                                //pre-select the first value of kendoui dropdownlist datasource
                                //for some reasons (maybe, bug) it always sends 0
                                //if no value has been selected (changed) in the dropdownlist
                                if (allManufacturers.length > 0) {
                                    e.model.ManufacturerId = allManufacturers[0].Id;
                                }
                            }
                        },
                        editable: {
                            confirmation: false,
                            mode: "inline"
                        },
                        scrollable: false,
                        columns: [{
                            field: "ManufacturerId",
                                title: "@T("Admin.Catalog.Products.Manufacturers.Fields.Manufacturer")",
                                width: 200,
                                editor: manufacturerDropDownEditor, 
                                template: "#:Manufacturer#"
                            },
                            @if (!Model.IsLoggedInAsVendor)
                            {
                                //vendor cannot edit this field
                                <text>{
                                field: "IsFeaturedProduct",
                                title: "@T("Admin.Catalog.Products.Manufacturers.Fields.IsFeaturedProduct")",
                                width: 200,
                                headerAttributes: { style: "text-align:center" },
                                attributes: { style: "text-align:center" },
                                template: '<img src="@Url.Content("~/Administration/Content/images/")active-#=IsFeaturedProduct#.gif" />'
                    },</text>
                            }
                        
                        {
                            field: "DisplayOrder",
                            title: "@T("Admin.Catalog.Products.Manufacturers.Fields.DisplayOrder")",
                            width: 200,
                            //integer format
                            format: "{0:0}"
                        }, {
                            command: [{
                                name: "edit",
                                text: "@T("Admin.Common.Edit")"
                            }, {
                                name: "destroy",
                                text: "@T("Admin.Common.Delete")"
                            }],
                            width: 200
                        }]
                    });
                });
                
                //local datasource
                var allManufacturers = [
                    @{
                        var manufacturerService = EngineContext.Current.Resolve<IManufacturerService>();
                        var allManufacturers= manufacturerService.GetAllManufacturers(showHidden: true);
                    }
                    @for (int i = 0; i < allManufacturers.Count; i++)
                    {
                        var manufacturer = allManufacturers[i];
                        <text>
                        {
                            Id: @(manufacturer.Id),
                            Name: "@(Html.Raw(HttpUtility.JavaScriptStringEncode(manufacturer.Name)))"
                        }
                        </text>
                        if (i != allManufacturers.Count - 1)
                        {
                            <text>,</text>
                        }
                    }
                ];

                function manufacturerDropDownEditor(container, options) {
                    $('<input required data-text-field="Name" data-value-field="Id" data-bind="value:ManufacturerId"/>')
                        .appendTo(container)
                        .kendoDropDownList({
                            autoBind: false,
                            dataSource: allManufacturers
                        });
                }
            </script>
        }
        else
        {
    @T("Admin.Catalog.Products.Manufacturers.NoManufacturersAvailable")
        }
    }
    else
    {
    @T("Admin.Catalog.Products.Manufacturers.SaveBeforeEdit")
    }